﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Citi.ReportManager.Domain;
using Citi.ReportManager.SchedulerUi;
using Citi.ReportManager.WebUi.Models;
using Citi.SecurityHelper;

namespace Citi.ReportManager.WebUi.Controllers
{
    public class ReportFolderController : Controller
    {
        public ActionResult Delete(int id)
        {
            var reportFolder = new ReportFolder().SelectById<ReportFolder>(id);
            reportFolder.Delete(reportFolder);

            var reportFolders = new ReportFolder().SelectAllActive<ReportFolder>();

            return PartialView("Index", new ReportFolderModel().CreateModel(reportFolders));
        }

        public ActionResult Form(int? id = 0)
        {
            if (!id.HasValue || id.Value == 0)
            {
                FillViewBags();
                return View();
            }

            var reportFolder = new ReportFolder().SelectById<ReportFolder>(id.Value);
            FillViewBags(reportFolder.CategoryId);

            return View(new ReportFolderModel().CreateModel(reportFolder));
        }

        public ActionResult Index()
        {
            var reportFolder = new ReportFolder().SelectAllActive<ReportFolder>();
            return View(new ReportFolderModel().CreateModel(reportFolder));
        }

        [HttpPost]
        public ActionResult Form(ReportFolderModel model)
        {
            if (model.Id == 0)
            {
                var domain = model.CreateDomain(model);
                domain.Save(domain);
            }
            else
            {
                var domain = new ReportFolder().SelectById<ReportFolder>(model.Id);
                domain.CanConvertToXlsb = model.CanConvertToXlsb;
                domain.CategoryId = model.CategoryId;
                domain.DaysToBackup = model.DaysToBackup;
                domain.FolderPath = model.FolderPath;
                domain.IncludeDirectories = model.IncludeDirectories;
                //domain.SeparateInFolder = model.SeparateInFolder;
                domain.MoveFilePermanently = model.MoveFilePermanently;

                domain.Save(domain);
            }

            return RedirectToAction("Index");
        }

        public ActionResult ManualUpload()
        {
            FillViewBags();
            return View();
        }

        [HttpPost]
        public ActionResult ManualUpload(ReportFolderModel model)
        {
            using (ImpersonateCitiUser.GetImpersonateUser(@"lac_bra_ftools", @"lac\lac_bra_ftools", "Ferrari3k10"))
            {
                model.Category = new CategoryModel { Id = model.CategoryId, Name = "Manual" };
                var domain = model.CreateDomain(model);

                new CopyFileLocal().StartCopy(new List<ReportFolder> { domain });

                return RedirectToAction("Index", "Report", new { categoryId = model.CategoryId });
            }
        }

        public void FillViewBags(int? categoryId = null)
        {
            ViewBag.Categories = new Category().SelectAllActive<Category>().Select(x =>
                new SelectListItem
                {
                    Text = x.Name,
                    Value = x.Id.ToString(),
                    Selected = categoryId.HasValue && categoryId.Value == x.Id
                });
        }
    }

    public class CopyFileLocal : CopyFile
    {
        public override string StepDescription { set { } }
    }
}
